package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.service.RoleService;
import com.atguigu.crowd.service.impl.RoleServiceImpl;
import com.atguigu.crowd.util.ResultEntity;
import com.atguigu.entity.Role;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;


    @RequestMapping("/role/delete/by/role/id/array")
    public ResultEntity<String> deleteByRoleIdArray(@RequestBody List<Integer> roleIds){
        roleService.removeRole(roleIds);
        return ResultEntity.successData();
    }


    @RequestMapping("/role/update")
    public ResultEntity<String> updateRole(Role role){
        roleService.updateRole(role);
        return ResultEntity.successData();
    }


    @RequestMapping("/role/save")
    public ResultEntity<String> saveRole(Role role){
        roleService.saveRole(role);

        return ResultEntity.successData();
    }



    @PreAuthorize("hasRole('部长')")
    @RequestMapping("/role/get/page/info")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageLimit",defaultValue = "5") Integer pageLimit,
            @RequestParam(value = "keyWord",defaultValue = "") String keyWord
    ){
        // 获取分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageLimit, keyWord);


        return ResultEntity.successResuleData(pageInfo);
    }

}
